import 'package:bot_toast/bot_toast.dart';
import 'package:cherry_toast/cherry_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat/common/service/services.dart';
import 'package:flutter_chat/common/style/styles.dart';
import 'package:flutter_chat/common/utils/utils.dart';
import 'package:flutter_chat/common/widget/widgets.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';

import 'logic.dart';

class TestPage extends StatelessWidget {
  TestPage({super.key});

  final logic = Get.put(TestLogic());
  final state = Get.find<TestLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: customAppBar("功能测试"),
      body: SingleChildScrollView(
        child: Wrap(
          spacing: 10,
          children: [
            Text(Jiffy.now().Hms),
            const Divider(height: 1),
            Text(Jiffy.now().yQQQQ),
            const Divider(height: 1),
            Text(Jiffy.now().yMMMMd),
            const Divider(height: 1),
            Text(Jiffy.now().yMMMdjm),
            const Divider(height: 1),
            Text(Jiffy.now().yMMMMdjm),
            const Divider(height: 1),
            Text(Jiffy.now().yMMMEdjm),
            const Divider(height: 1),
            Text(Jiffy.now().yMMMMEEEEd),
            const Divider(height: 1),
            Text(Jiffy.now().yMMMMEEEEdjm),
            const Divider(height: 1),
            Obx(() {
              return Text("正在运行的服务数量：${RetryService.to.serviceCount}");
            }),
            ElevatedButton(
              onPressed: () {
                RetryService.to.startService("a", () async {
                  LogUtil.p("重试服务a运行 ${Jiffy.now().Hms}");
                  return false;
                }, interval: const Duration(seconds: 3));
              },
              child: const Text("创建重试服务a"),
            ),
            ElevatedButton(
              onPressed: () {
                RetryService.to.stopService("a");
              },
              child: const Text("停止重试服务a"),
            ),
            ElevatedButton(
              onPressed: () {
                RetryService.to.startService("b", () async {
                  LogUtil.p("重试服务b运行 ${Jiffy.now().Hms}");
                  return false;
                }, interval: const Duration(seconds: 3));
              },
              child: const Text("创建重试服务b"),
            ),
            ElevatedButton(
              onPressed: () {
                RetryService.to.stopService("b");
              },
              child: const Text("停止重试服务b"),
            ),
            ElevatedButton(
              onPressed: () {
                RetryService.to.startService("c", () async {
                  LogUtil.p("重试服务c运行 ${Jiffy.now().Hms}");
                  return false;
                }, interval: const Duration(seconds: 3));
              },
              child: const Text("创建重试服务c"),
            ),
            ElevatedButton(
              onPressed: () {
                RetryService.to.stopService("c");
              },
              child: const Text("停止重试服务c"),
            ),
            ElevatedButton(
              onPressed: () {
                RetryService.to.stopAllServices();
              },
              child: const Text("停止所有重试服务"),
            ),
            const Divider(height: 1),
            Obx(() {
              return Text("连接状态：${WebSocketService.to.status}");
            }),
            ElevatedButton(
              onPressed: () {
                WebSocketService.to.open();
              },
              child: const Text("打开WebSocket连接"),
            ),
            ElevatedButton(
              onPressed: () {
                WebSocketService.to.closed();
              },
              child: const Text("关闭WebSocket连接"),
            ),
            ElevatedButton(
              onPressed: () {
                WebSocketService.to.closeAndStopReconnect();
              },
              child: const Text("关闭连接并停止重连"),
            ),
            ElevatedButton(
              onPressed: () {
                // WebSocketService.to.addListener(logic.webSocketListener);
              },
              child: const Text("添加WebSocket订阅者"),
            ),
            ElevatedButton(
              onPressed: () {
                // WebSocketService.to.removeListener(logic.webSocketListener);
              },
              child: const Text("移除WebSocket订阅者"),
            ),
            ElevatedButton(
              onPressed: () {
                WebSocketService.to.receptionMessage("这是一条本地消息");
              },
              child: const Text("给WebSocket订阅者发信息"),
            ),
            ElevatedButton(
              onPressed: () {
                WebSocketService.to.sendMessage("这是一条云端消息");
              },
              child: const Text("给WebSocket服务端发信息"),
            ),
            const Divider(height: 1),
            _buildShowNotification(),
            _buildShowText(),
            ElevatedButton(
              onPressed: () {
                ToastUtil.closeAllLoading();
              },
              child: const Text("关闭所有吐司加载"),
            ),
            ElevatedButton(
              onPressed: () {
                CherryToast.success(
                  title: const Text(
                    "最简单的樱桃吐司",
                    style: TextStyle(
                      color: Colors.black,
                    ),
                  ),
                ).show(context);
              },
              child: const Text("简单的樱桃吐司"),
            ),

            const Divider(height: 1),

            /// 无图片
            const ImageWidget(
              "",
              size: 50,
              radius: 10,
            ),

            /// 网络图片
            const ImageWidget(
              "http://q1.qlogo.cn/g?b=qq&nk=2327972001&s=640",
              size: 50,
              radius: 10,
            ),

            /// 本地图片
            const ImageWidget(
              "assets/images/elaborated-time@1000x1000px.jpg",
              size: 50,
              radius: 10,
            ),

            /// 文件图片
            const ImageWidget(
              "/data/data/com.zym.flutter_chat/app_flutter/wallhaven-9dgpxd.png",
              size: 50,
              radius: 10,
            ),
          ],
        ),
      ),
    );
  }

  ElevatedButton _buildShowText() {
    return ElevatedButton(
      onPressed: () {
        BotToast.showText(
          text: "这是一个文字Toast",
          textStyle: const TextStyle(
            fontSize: AppTheme.middleFont,
            color: Colors.white,
          ),
          // 展示时长
          duration: const Duration(seconds: 3),
          // 动画延迟
          animationDuration: const Duration(milliseconds: 350),
          // 动画反向延迟
          animationReverseDuration: const Duration(milliseconds: 350),
        );
      },
      child: const Text("弹出文字Toast"),
    );
  }

  ElevatedButton _buildShowNotification() {
    return ElevatedButton(
      onPressed: () {
        ToastUtil.warningNotification(
          title: "提醒",
        );
      },
      child: const Text(
        "弹出简单通知Toast",
      ),
    );
  }
}
